home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / perl / doc / parr < prev    next >
Encoding:
Text File  |  1991-07-29  |  3.4 KB  |  165 lines

  1. #!/usr/bin/perl
  2.  
  3. # @(#)@ parr 4.2.1
  4.  
  5. # rearrange conforming PS code to print the pages in an arbitrary
  6. # order.  The -o option takes a list of ranges, like this:
  7. #    1-5    1-10,11-20    11-,1-10
  8. # usage: parr [-a4] [-o list] [file]
  9. #
  10. # jgreely@cis.ohio-state.edu, 89/10/23
  11. # modified by jv@mh.nl, 91/078/15
  12.  
  13. $order='';
  14. $signFlag='';
  15. $signCount=0;
  16. $DEBUG=0;
  17. $TMPDIR='/usr/tmp';
  18.  
  19. while ($_ = $ARGV[0],/^-/) {
  20.     shift;
  21.     last if /^-\-$/;
  22.     /^-o/ && ($order = shift,next);
  23.     /^-a4/ && ($a4flag++,next);
  24.     die "usage: parr [-a4] [-o list] [file]\n";
  25. }
  26.  
  27. $file = "$TMPDIR/p$$.header";
  28. @files = ($file);
  29. $sheet=0;
  30. open(FILE,">$file") || die "$file: $!\n";
  31. while (<>) {
  32.     #
  33.     # hack to use NeXT Preview: strip old '%%Pages:' lines
  34.     #
  35.     next if /^%%Pages:/;
  36.     if (/^%%Page:/) {
  37.         $sheet++;
  38.         $pagemap{$sheet} = $1 if /%%Page:\s+(\S+)\s+\S+/;
  39.         close(FILE);
  40.         $file = "$TMPDIR/p$$.$sheet";
  41.         push(@files,$file);
  42.         open(FILE,">$file") || die "$file: $!\n";
  43.     }
  44.     if (/^%%Trailer/) {
  45.         close(FILE);
  46.         $file = "$TMPDIR/p$$.trailer";
  47.         push(@files,$file);
  48.         open(FILE,">$file") || die "$file: $!\n";
  49.     }
  50.     if (/^TeXDict begin @(a4|letter)/) {
  51.         # Insert twoup before switching to TeXDict
  52.         &twoup;
  53.         $twoup++;
  54.         print FILE "TeXDict begin @landscape\n";
  55.         next;
  56.     }
  57.     print FILE $_;
  58. }
  59. close(FILE);
  60. die "twoup insertion error\n" unless $twoup == 1;
  61.  
  62. @order = ();
  63. if ($order) {
  64.     foreach $range (split(/,/,$order)) {
  65.         ($start,$sep,$end) = split(/(-)/,$range);
  66.         $start = 1 unless $start;
  67.         $end = $sheet unless $end;
  68.         if ($sep) {
  69.             push(@order,$start..$end);
  70.         }else{
  71.             push(@order,$start);
  72.         }
  73.     }
  74. }elsif ($signFlag) {
  75.     if (! $signCount) {
  76.         $signCount = $sheet;
  77.         $signCount += (4 - $sheet % 4) if ($sheet % 4);
  78.     }else{
  79.         $signCount *=4;
  80.     }
  81.     for($base=0;$base<$sheet;$base+=$signCount) {
  82.         @tmp = ($signCount/2+$base);
  83.         push(@tmp,$tmp[0]+1,$tmp[0]+2,$tmp[0]-1);
  84.         while ($tmp[3] > $base) {
  85.             push(@order,@tmp);
  86.             @tmp = ($tmp[0]-2,$tmp[1]+2,$tmp[2]+2,$tmp[3]-2);
  87.         }
  88.     }
  89. }else{
  90.     @order = (1..$sheet);
  91. }
  92.  
  93. @tmp=@order;
  94. @order=();
  95. foreach $page (@tmp) {
  96.     push(@order,$page > $sheet ? "B" : $page);
  97. }
  98.  
  99. open(FILE,"$TMPDIR/p$$.header");
  100. $_ = <FILE>;
  101. print $_,"%%Pages: (atend)\n";
  102. print while <FILE>;
  103. close(FILE);
  104.  
  105. foreach $page (@order) {
  106.     $count++;
  107.     $num = "?";
  108.     $num = $pagemap{$page} if defined $pagemap{$page};
  109.     $num .= "/" . $pagemap{$order[$count]} 
  110.         if defined $pagemap{$order[$count]};
  111.     print "%%Page: $num ", ($count+1)/2, "\n" if $count & 1;
  112.     print "%%OldPage: $page\n";
  113.     if ($page eq "B") {
  114.         print "showpage\n";
  115.     }else{
  116.         open(FILE,"$TMPDIR/p$$.$page");
  117.         while (<FILE>) {
  118.             print unless /^%%Page:/;
  119.         }
  120.         close(FILE);
  121.     }
  122. }
  123. open(FILE,"$TMPDIR/p$$.trailer");
  124. print while <FILE>;
  125. close(FILE);
  126. print "%%Pages: $count\n";
  127.  
  128. unlink @files unless $DEBUG;
  129. exit(0);
  130.  
  131. sub twoup {
  132. $factor = 0.707106781187;
  133.  
  134. # Measurements are in 1/100 inch approx.
  135. if ( $a4flag) {
  136.     $topmargin = -57;
  137.     $leftmargin = 28;
  138.     $othermargin = 445;    # do not change -- relative to $leftmargin
  139. }
  140. else {
  141.     $topmargin = -30;
  142.     $leftmargin = 0;
  143.     $othermargin = 445;    # do not change -- relative to $leftmargin
  144. }
  145. $vsize = -1 - $factor;
  146. print FILE <<EOD;
  147. /isoddpage true def
  148. /orig-showpage /showpage load def
  149. /showpage {
  150.         isoddpage not { orig-showpage } if
  151.         /isoddpage isoddpage not store 
  152.     } def
  153.  
  154. /bop-hook {
  155.     /vsize $vsize def
  156.         isoddpage 
  157.     { $factor $factor scale $topmargin $leftmargin translate }
  158.         { 0 $othermargin translate}
  159.     ifelse
  160.     } def
  161.  
  162. /end-hook{ isoddpage not { orig-showpage } if } def
  163. EOD
  164. }
  165.